<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/4/1 13:30
 * @version
 */
namespace Modules\Admin\Main\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Goods;
use App\Models\GoodsClass;
use App\Models\GoodsSku;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class GoodsController extends Controller
{
    public function search(Request $request)
    {
        return view('main::goods_list',$request->all());
    }

    public function apiSearch(Request $request)
    {
        $limit = $request->input('limit', 10); // 这个是接受参数
        $offset = $request->input('offset', 0);
        $field_type = $request->input('field_type');
        $keyword    = $request->input('keyword');
        $goods_type = $request->input('goods_type');
        $data = [];
        $query = Goods::query();
        if($field_type && !empty($keyword)){
            if($field_type=='goods_id'){
                $query = $query->where('id',$keyword);
            }
            if($field_type=='goods_name'){
                $query = $query->where('goods_name','like','%' . $keyword .'%');
            }
        }
        if ($goods_type=='hot'){
            $query = $query->where('hot',1);
        }
        if ($goods_type=='selected'){
            $query = $query->where('selected',1);
        }

        $data['total'] = $query->count();
        $data['items'] = [];
        $goods = $query->take($limit)
            ->skip($offset)
            ->orderBy('id', 'desc')
            ->get();
        if ($data['total']>0) {
            foreach ($goods as $item) {
                $item['sku'] = GoodsSku::where("goods_id",$item['id'])->get();
                array_push($data['items'],$item);
            }
        }
        return success($data);
    }

    public function apiAdd(Request $request)
    {
            $input = $request->only(['goods_name','class_id','type_id','sell_price','original_price']);
            $input['image'] = implode(',',$request->input('image'));
            $input['detail'] = implode(',',$request->input('detail'));
            $input['header'] = implode(',',$request->input('header'));
            $input['store_num'] = $request->input('store_num',0);
            $input['hot'] = intval($request->input('hot'),0);
            $input['selected'] = intval($request->input('selected'),0);
            $input['is_onsale'] = intval($request->input('is_onsale'),0);
            $input['header'] = implode(',',$request->input('header'));

            $sku = $request->input("sku");

            $input['class_name'] = GoodsClass::query()->where('id',$input['class_id'])->value('class_name');
            $input['type_name'] = GoodsClass::query()->where('id',$input['type_id'])->value('class_name');
            Log::info($input);
            $goods_id = Goods::query()->insertGetId($input);
            if($goods_id){
                if (empty($sku) || !isset($sku)){
                    $skuValue = ["goods_id" => $goods_id, "sku_name" => "标准", "sku_price" => $input['sell_price'], "created_at" => Carbon::now()];
                    GoodsSku::query()->insertGetId($skuValue);
                }else {
                    foreach ($sku as $item) {
                        $skuValue = ["goods_id" => $goods_id, "sku_name" => $item['sku_name'], "sku_price" => $item['sku_price'], "created_at" => Carbon::now()];
                        GoodsSku::query()->insertGetId($skuValue);
                    }
                }
            }

            Log::info('add goods',$input);
            return success();
    }

    public function apiUpdate(Request $request)
    {
        $goods_id = $request->input('id');
        $input = $request->only(['goods_name','class_id','type_id','sell_price','original_price']);
        $input['image'] = implode(',',$request->input('image'));
        $input['detail'] = implode(',',$request->input('detail'));
        $input['header'] = implode(',',$request->input('header'));
        $input['store_num'] = $request->input('store_num',0);
        $input['hot'] = intval($request->input('hot'),0);
        $input['selected'] = intval($request->input('selected'),0);
        $input['is_onsale'] = intval($request->input('is_onsale'),0);
        //$input['header'] = $request->input('header');
        $sku = $request->input("sku");
        $input['class_name'] = GoodsClass::query()->where('id',$input['class_id'])->value('class_name');
        $input['type_name'] = GoodsClass::query()->where('id',$input['type_id'])->value('class_name');
        DB::beginTransaction();
        try {
            Goods::query()->where('id', $goods_id)->update($input);
            if (count($sku)>0) {
                foreach ($sku as $item) {
                    if (array_key_exists('id', $item)) {
                        $skuData = GoodsSku::where("id", $item['id'])->first();
                        $skuData->sku_name = $item['sku_name'];
                        $skuData->sku_price = $item['sku_price'];
                        $skuData->save();
                    } else {
                        $skuValue = ["goods_id" => $goods_id, "sku_name" => $item['sku_name'], "sku_price" => $item['sku_price'], "created_at" => Carbon::now()];
                        GoodsSku::query()->insertGetId($skuValue);
                    }
                }
            }
            DB::commit();
        }catch(\Exception $e){
            DB::rollBack();
        }
        return success();

    }

    public function apiDelete(Request $request)
    {
        $id = $request->input('id');
        if($id){
            Goods::query()->where('id',$id)->delete();
            GoodsSku::query()->where("goods_id",$id)->delete();
        }
        return success();
    }

    public function searchClass(Request $request)
    {
        return view('main::goods_class_list',$request->all());
    }

    public function apiSearchClass(Request $request)
    {
        $data = [];
        $data['total'] = GoodsClass::query()->count();
        $data['items'] = GoodsClass::query()->with('image')->get();
        return success($data);
    }

    public function apiChildClass(Request $request)
    {
        $pid = $request->input('pid',0);
        $data = GoodsClass::query()->where('pid',$pid)->get();
        return success($data);
    }

    public function apiAddCategory(Request $request)
    {
        $input = $request->only(['class_name','pid','image_id']);
        $input['sort'] = $request->input('sort',50);
        $cid = GoodsClass::query()->insertGetId($input);
        if($cid) return success('添加分类成功');
    }

    public function apiUpdateCategory(Request $request)
    {
        $cid = $request->input('id');
        $input = $request->only(['class_name','pid','image_id']);
        $input['sort'] = $request->input('sort',50);
        if($cid) {
            GoodsClass::query()->where('id',$cid)->update($input);
        }
        return success('添加分类成功');
    }

    public function apiDeleteCategory(Request $request)
    {
        $id = $request->input('id');
        if($id){
            GoodsClass::query()->where('id',$id)->delete();
        }
        return success();
    }

    public function apiDeleteGoodsSku(Request $request){
        $id = $request->input("id");
        if ($id){
            GoodsSku::query()->where('id',$id)->delete();
        }
        return success();
    }

}